package showgo.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import showgo.modell.Manager;
import showgo.modell.inszenierung.Darstellerposten;
import showgo.modell.inszenierung.Inszenierung;
import showgo.modell.inszenierung.Nichtdarstellerposten;
import showgo.modell.stamm.Ensemble;
import showgo.modell.stamm.Person;
import showgo.modell.stueck.Stueck;

/**
 * Dieser Dailog dient dazu beim Anlegen einer neuen Inszenierung ein
 * Theaterstueck zu waehlen, woraus dann automatisch eine Kopie erstellt wird;*
 * 
 * @author lutz
 * 
 */
public class InszStueckDialog extends JDialog implements ActionListener,
	ListSelectionListener {

    /**
     * 
     */
    private static final long serialVersionUID = -8921475171709165923L;

    private javax.swing.JButton abbrechenButton;
    private javax.swing.JButton beendenButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    @SuppressWarnings("rawtypes")
    private JList inszStueckList;

    public InszStueckDialog() {
	setModal(true);
	setTitle("Theaterstueck fuer Inszenierung");

	initComponents();
	fillList();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void fillList() {
	if (!Manager.getInstance().getStuecke().isEmpty()) {
	    DefaultListModel daten = new DefaultListModel();
	    if (!Manager.getInstance().getStuecke().isEmpty()) {
		for (int i = 0; i < Manager.getInstance().getStuecke().size(); i++) {
		    daten.add(i, Manager.getInstance().getStuecke().get(i)
			    .getName());
		}
	    }
	    inszStueckList.setModel(daten);
	}
    }

    @SuppressWarnings("rawtypes")
    private void initComponents() {
	setPreferredSize(new java.awt.Dimension(500, 300));
	// DialogFrame kann nicht maximiert werden
	setResizable(false);

	jLabel1 = new javax.swing.JLabel();
	beendenButton = new javax.swing.JButton();
	abbrechenButton = new javax.swing.JButton();

	jLabel2 = new javax.swing.JLabel();
	jLabel3 = new javax.swing.JLabel();

	setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

	jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
	jLabel1.setText("Auswahl von Theaterstuecken");

	beendenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	beendenButton.setText("Fertig");
	beendenButton.addActionListener(this);

	abbrechenButton.setFont(new java.awt.Font("Tahoma", 1, 12));
	abbrechenButton.setText("Abbrechen");
	abbrechenButton.addActionListener(this);

	jScrollPane1 = new javax.swing.JScrollPane();
	inszStueckList = new JList();
	inszStueckList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	inszStueckList.addListSelectionListener(this);
	jScrollPane1.setViewportView(inszStueckList);

	jLabel2.setText("Bitte waehlen Sie das Theaterstueck aus, das Sie fuer die Inszenierung ");

	jLabel3.setText("verwenden wollen.");

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
		getContentPane());
	getContentPane().setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(
											jLabel3)
										.addComponent(
											jScrollPane1,
											javax.swing.GroupLayout.PREFERRED_SIZE,
											152,
											javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									Short.MAX_VALUE)
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING,
										false)
										.addComponent(
											abbrechenButton,
											javax.swing.GroupLayout.Alignment.TRAILING,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											Short.MAX_VALUE)
										.addComponent(
											beendenButton,
											javax.swing.GroupLayout.Alignment.TRAILING,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											javax.swing.GroupLayout.DEFAULT_SIZE,
											Short.MAX_VALUE)))
						.addGroup(
							layout.createSequentialGroup()
								.addGroup(
									layout.createParallelGroup(
										javax.swing.GroupLayout.Alignment.LEADING)
										.addComponent(
											jLabel1)
										.addComponent(
											jLabel2))
								.addGap(0,
									5,
									Short.MAX_VALUE)))
				.addContainerGap()));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addComponent(jLabel1)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jLabel2)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(jLabel3)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
							layout.createSequentialGroup()
								.addGap(18, 18,
									18)
								.addComponent(
									jScrollPane1,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									javax.swing.GroupLayout.DEFAULT_SIZE,
									javax.swing.GroupLayout.PREFERRED_SIZE))
						.addGroup(
							layout.createSequentialGroup()
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED,
									80,
									Short.MAX_VALUE)
								.addComponent(
									abbrechenButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									35,
									javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									beendenButton,
									javax.swing.GroupLayout.PREFERRED_SIZE,
									35,
									javax.swing.GroupLayout.PREFERRED_SIZE)))
				.addContainerGap(20, Short.MAX_VALUE)));

	pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == beendenButton) {
	    // Erst pruefen, ob ein Stueck aus der Liste gewaehlt wurde;
	    if (inszStueckList.getSelectedIndex() > -1) {
		Stueck gewStueck = Manager.getInstance().getStuecke()
			.get(inszStueckList.getSelectedIndex());

		Stueck stueck = null;
		// Pruefen, ob das Stueck eine Kopie ist; Wenn nicht, dann eine
		// erstellen;
		if (!gewStueck.getName().contains("Kopie")) {
		    stueck = gewStueck.tiefeKopie();
		    Manager.getInstance().fuegeStueckHinzu(gewStueck);
		} else {
		    stueck = gewStueck;
		}

		List<Person> gruppe = new ArrayList<Person>();
		Ensemble ensemble = new Ensemble("neu", gruppe);
		List<Darstellerposten> darstellerposten = new ArrayList<Darstellerposten>();
		List<Nichtdarstellerposten> nichtDarsteller = new ArrayList<Nichtdarstellerposten>();
		Inszenierung insz = new Inszenierung("neue Inszenierung",
			stueck, ensemble, darstellerposten, nichtDarsteller);
		final InszenierungDialog inszDialog = new InszenierungDialog(
			insz);
		inszDialog.setModal(true);
		inszDialog.setLocationRelativeTo(null);
		inszDialog.setVisible(true);
		dispose();
	    } else {
		JOptionPane.showMessageDialog(null,
			"Bitte waehlen Sie erst ein Stueck aus der Liste!");
	    }
	} else if (e.getSource() == abbrechenButton) {
	    int ret = JOptionPane.showConfirmDialog(null,
		    "Wollen Sie wirklich abbrechen?",
		    "Abbruch der Inszenierung!", JOptionPane.YES_NO_OPTION);
	    if (ret == JOptionPane.YES_OPTION) {
		dispose();
	    }
	}

    }

    @Override
    public void valueChanged(ListSelectionEvent arg0) {
	// TODO Auto-generated method stub

    }

}
